package com.miyuan.view.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.miyuan.R;
import com.miyuan.base.BaseFragment;
import com.miyuan.bean.ResultBean;
import com.miyuan.bean.respone.GoodsListBean;
import com.miyuan.net.NetService;
import com.miyuan.net.RetrofitClient;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

//补货清单
public class GoodsListFragment extends BaseFragment {

    @BindView(R.id.goods_list)
    RecyclerView recyclerView;

    //构造函数
    public static GoodsListFragment newInstance() {
        GoodsListFragment goodsListFragment = new GoodsListFragment();
        goodsListFragment.setArguments(new Bundle());
        return goodsListFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setFragmentLayout(R.layout.fragment_goods);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        loadingDialog.show();
        RetrofitClient.getInstance()
                .create(NetService.class)
                .getReplenishmentList()
                .enqueue(new Callback<ResultBean<List<GoodsListBean>>>() {

                    @Override
                    public void onResponse(Call<ResultBean<List<GoodsListBean>>> call, Response<ResultBean<List<GoodsListBean>>> response) {
                        loadingDialog.dismiss();
                        ResultBean<List<GoodsListBean>> resultBean = response.body();
                        if (resultBean.getData() != null) {
                            initList(resultBean.getData());
                        } else {
                            Toast.makeText(getActivity(), resultBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultBean<List<GoodsListBean>>> call, Throwable t) {
                        loadingDialog.dismiss();
                    }
                });

    }

    public void initList(List list) {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.item_goods, new SlimInjector<GoodsListBean>() {
            @Override
            public void onInject(final GoodsListBean data, IViewInjector injector) {
                injector.text(R.id.goods_item_text, data.getGoodsName());
                injector.text(R.id.goods_item_count, data.getTotal() + "");
                injector.image(R.id.goods_item_icon, R.mipmap.machine_default);
            }
        }).attachTo(recyclerView);
        slimAdapter.updateData(list);
    }
}
